<?php

namespace app\common\model\xilujob;

use think\Exception;
use think\Model;
use function fast\array_get;


class CompanyAddress extends Model
{
    // 表名
    protected $name = 'xilujob_company_address';

    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;


    public function province() {
        return $this->belongsTo('Area','province_id');
    }
    public function city() {
        return $this->belongsTo('Area','city_id');
    }

    public function district() {
        return $this->belongsTo('Area','district_id');
    }


    public static function addAddress($params) {
        $hasComment = self::where('user_id',$params['user_id'])
                ->where('company_id', $params['company_id'])
                ->where('province_id',$params['province_id'])
                ->where('city_id',$params['city_id'])
                ->where('district_id',$params['district_id'])
                ->where('address_name',$params['address_name'])
                ->where('address',$params['address'])
                ->count()>0;
        if($hasComment) {
            throw new Exception('已存在');
        }
        try {
            $comment = new self();
            $ret = $comment->allowField(true)->save($params);
            if ($ret === false) {
                throw new Exception($comment->getError());
            }
            return self::get($comment->id);
        }catch (\Exception $e) {
            return $e;
        }

    }
}
